package com.pactera.asmp.server.common.logprint.aspect;

import com.pactera.asmp.server.common.constants.MagicNumberConstant;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;

/**
 * <p>Class: com.lzq.plugin.logprint.aspect.PluginLogAspect</p>
 * <p>Description: </p>
 * <pre>
 *
 * </pre>
 *
 * @author lv zhiqiang
 * @date 2019/7/23/11:23
 */
@Aspect
public class PluginLogAspect extends BaseLogAspect{
    private static final String BEFORE_STRING = "[AOP][PLUGIN][BEFORE] ";
    private static final String AFTER_STRING = "[AOP][PLUGIN][AFTER] ";

    /**
     * Pointcut method.
     */
    @Pointcut("execution(public * com.pactera.asmp.server.controller.*.*(..))")
    @Order(MagicNumberConstant.ONE)
    public void pointcutControllerMethod() {
    }

    /**
     * Pointcut method.
     */
    @Pointcut("execution(public * com.pactera.asmp.server.common.threadpool.task.*.*(..))")
    @Order(MagicNumberConstant.ONE)
    public void pointcutTaskMethod() {
    }

    /**
     * Run data collection object.
     *
     * @param proceedingJoinPoint the proceeding join point
     * @return the object
     * @throws Throwable the throwable
     */
    @Around("pointcutControllerMethod() || pointcutTaskMethod()")
    public Object injectApi(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getName();
        Object[] args = proceedingJoinPoint.getArgs();
        addLogQueue(className, methodName, BEFORE_STRING, args);
        Object result = proceedingJoinPoint.proceed();
        addLogQueue(className, methodName, AFTER_STRING, result);
        return result;
    }
}
